<?
    require_once("Net/IPv4.php");
    
    abstract class Principio_Controller_Action extends PrincipioLab_Controller_Action
    {
        public static $upload_errors = array(
            UPLOAD_ERR_OK => "No errors.",
            UPLOAD_ERR_INI_SIZE => "Larger than upload_max_filesize.",
            UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
            UPLOAD_ERR_PARTIAL => "Partial upload.",
            UPLOAD_ERR_NO_FILE => "No file.",
            UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
            UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
            UPLOAD_ERR_EXTENSION => "File upload stopped by extension."
          );


        public function checkPostItem($item_name)
        {
            $is_valid = Principio_Common::checkValidity(isset($this->post_data[$item_name]), "Script problem: there is no [".$item_name."] parameter in the request.");
            if (!$is_valid)
                $this->post_data[$item_name] = "";

            return $is_valid;
        }


        public function checkPostItemString($item_name, $item_name_public)
        {
            return ($this->checkPostItem($item_name) && Principio_Common::checkValidity($this->post_data[$item_name] != "", $item_name_public." can't be empty."));
        }


        public function checkPostItemInt($item_name, $item_name_public)
        {
            return (
                $this->checkPostItem($item_name)
                && $this->checkPostItemString($item_name, $item_name_public)
                && Principio_Common::checkValidity(intval($this->post_data[$item_name]) > 0, $item_name_public." must be greater 0 or it isn't a valid integer.")
            );
        }


        public function checkPostItemCheckbox($item_name, $item_name_public)
        {
            return (
                $this->checkPostItemInt($item_name, $item_name_public)
                && Principio_Common::checkValidity(($this->post_data[$item_name] == 1) || ($this->post_data[$item_name] == 2), $item_name_public." must be checked or unchecked.")
            );
        }


        public function checkPostItemIP($item_name, $item_name_public)
        {
            $net_IPv4 = new Net_IPv4();

            return ($this->checkPostItem($item_name) && Principio_Common::checkValidity($net_IPv4->validateIP($this->post_data[$item_name]), $item_name_public." is not a valid IP."));
        }


        public function checkFileItem($file_name)
        {
            if (Principio_Common::checkValidity((count($_FILES) > 0) && (isset($_FILES[$file_name])), "You have to select ".$file_name." for upload."))
            {
                Principio_Common::checkValidity((intval($_FILES[$file_name]['size']) > 0), $file_name." size is 0. This may be caused by hosting error.");
                Principio_Common::checkValidity((intval($_FILES[$file_name]['error']) == 0), "Error while uploading: [".self::$upload_errors[$_FILES[$file_name]['error']]."]. You have to contact your hosting provider or to check PHP settings.");
            };

            return !Principio_Common::$is_error;
        }


        public function checkPostItemSetting(Principio_Model_Setting $model_setting)
        {
            $check_result = $this->checkPostItem($model_setting->getCaption());
            $model_setting->setValue($this->post_data[$model_setting->getCaption()]);

            return $check_result;
        }


        public function checkPostItemSettingString(Principio_Model_Setting $model_setting, $item_name_public)
        {
            $check_result = $this->checkPostItemString($model_setting->getCaption(), $item_name_public);
            $model_setting->setValue($this->post_data[$model_setting->getCaption()]);

            return $check_result;
        }


        public function checkPostItemSettingIP(Principio_Model_Setting $model_setting, $item_name_public)
        {
            $check_result = $this->checkPostItemIP($model_setting->getCaption(), $item_name_public);
            $model_setting->setValue($this->post_data[$model_setting->getCaption()]);

            return $check_result;
        }


        public function checkPostItemSettingInt(Principio_Model_Setting $model_setting, $item_name_public)
        {
            $check_result = $this->checkPostItemInt($model_setting->getCaption(), $item_name_public);
            $model_setting->setValue($this->post_data[$model_setting->getCaption()]);

            return $check_result;
        }


        public function checkPostItemSettingCheckbox(Principio_Model_Setting $model_setting, $item_name_public)
        {
            $check_result = $this->checkPostItemCheckbox($model_setting->getCaption(), $item_name_public);
            $model_setting->setValue($this->post_data[$model_setting->getCaption()] == 2 ? "Yes" : "No");

            return $check_result;
        }
    }
?>
